/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ThemPhieuXuatHangInternalFrame.java
 *
 * Created on Jul 20, 2010, 4:04:01 PM
 */

package GUI;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author sv
 */
 public class ThemPhieuXuatHangInternalFrame extends javax.swing.JInternalFrame {

    //ArrayList<HoaDonPOJO> list = new ArrayList<HoaDonPOJO>();
    double tongTien=0;



    public void hienThiDanhSachDaiLy(){
//        ArrayList<DaiLyPOJO> list = DaiLyBUS.selectDanhSachDaiLy();
//        //Đọc dữ liệu
//        DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
//        for(int i=0; i<list.size(); i++){
//            //Đọc từng dòng
//            Vector ct = new Vector();
//            ct.add(list.get(i).getMaDaiLy());
//            ct.add(list.get(i).getTenDaiLy());
//            //Thêm một dòng vào model của JComboBox
//            dcbm.addElement(ct);
//        }
//        //Gán model cho JComboBox
//        this.jcbTenDaiLy.setModel(dcbm);
    }

//    public void hienThiDanhSachMatHang(ArrayList<HoaDonPOJO> list){
        //Tạo các cột trên jtbDanhSach
//        DefaultTableModel dtm = new DefaultTableModel();
//        dtm.addColumn("STT");
//        dtm.addColumn("Mặt Hàng");
//        dtm.addColumn("Đơn Vị Tính");
//        dtm.addColumn("Số Lượng");
//        dtm.addColumn("Đơn Giá");
//        dtm.addColumn("Thành Tiền");
//
//        //Tạo model
//        dtm.setRowCount(list.size());
//        for(int i=0; i<list.size(); i++){
//            dtm.setValueAt(i+1, i, 0);
//            dtm.setValueAt(list.get(i).getTenMatHang(), i, 1);
//            dtm.setValueAt(list.get(i).getDonviTinh(), i, 2);
//            dtm.setValueAt(list.get(i).getSoLuong(), i, 3);
//            dtm.setValueAt(list.get(i).getDonGia(), i, 4);
//            dtm.setValueAt(list.get(i).getThanhTien(), i, 5);
//        }
//
//        //Gán model cho JTable
//        this.jtbDanhSach.setModel(dtm);
//        this.jtbDanhSach.changeSelection(0, 0, false, false);
//    }
    /** Creates new form ThemPhieuXuatHangInternalFrame */
    public ThemPhieuXuatHangInternalFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jlbTenDaiLy = new javax.swing.JLabel();
        jcbTenDaiLy = new javax.swing.JComboBox();
        jlbNgayLapPhieu = new javax.swing.JLabel();
        jtfNgayLapPhieu = new javax.swing.JTextField();
        jbtThem = new javax.swing.JButton();
        jbtSua = new javax.swing.JButton();
        jbtXoa = new javax.swing.JButton();
        jbtThoat = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSach = new javax.swing.JTable();
        jlbTongTien = new javax.swing.JLabel();
        jtfTongTien = new javax.swing.JTextField();
        jbtChapNhan = new javax.swing.JButton();

        jButton1.setText("jButton1");

        setClosable(true);
        setForeground(java.awt.Color.white);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("THEM PHIEU XUAT HANG");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jlbTenDaiLy.setText("Tên cửa hàng :");

        jlbNgayLapPhieu.setText("Ngày Lập Phiếu :");

        jbtThem.setText("Thêm");
        jbtThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThemActionPerformed(evt);
            }
        });

        jbtSua.setText("Sửa");
        jbtSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtSuaActionPerformed(evt);
            }
        });

        jbtXoa.setText("Xóa");
        jbtXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtXoaActionPerformed(evt);
            }
        });

        jbtThoat.setText("Thoát");
        jbtThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtThoatActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 24)); // NOI18N
        jLabel1.setText("LẬP PHIẾU XUẤT HÀNG");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh Sách Mặt Hàng"));

        jtbDanhSach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jtbDanhSach);

        jlbTongTien.setText("Tổng Tiền :");

        jtfTongTien.setEditable(false);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 424, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jlbTongTien)
                        .addGap(18, 18, 18)
                        .addComponent(jtfTongTien, javax.swing.GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbTongTien)
                    .addComponent(jtfTongTien, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        jbtChapNhan.setText("Chấp Nhận");
        jbtChapNhan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtChapNhanActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jbtThem, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(jbtSua, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(jbtXoa, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(jbtThoat, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE)
                            .addComponent(jbtChapNhan, javax.swing.GroupLayout.DEFAULT_SIZE, 87, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(137, 137, 137))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jlbTenDaiLy)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jcbTenDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, 124, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47)
                        .addComponent(jlbNgayLapPhieu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jtfNgayLapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(99, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlbTenDaiLy)
                    .addComponent(jcbTenDaiLy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlbNgayLapPhieu)
                    .addComponent(jtfNgayLapPhieu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addComponent(jbtThem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtSua)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtXoa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jbtChapNhan)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jbtThoat)
                        .addGap(29, 29, 29))))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.hienThiDanhSachDaiLy();
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
        String d = sdf.format(now);
        this.jtfNgayLapPhieu.setText(d);

        //ArrayList<HoaDonPOJO> hd = new ArrayList<HoaDonPOJO>();
        //this.hienThiDanhSachMatHang(hd);
    }//GEN-LAST:event_formInternalFrameOpened

    private void jbtThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThoatActionPerformed
        // TODO add your handling code here:
        int reply = JOptionPane.showConfirmDialog(this, "Bạn có muốn thoát", "Thông báo", WIDTH);
        if(reply == JOptionPane.OK_OPTION){
            this.dispose();
        }
    }//GEN-LAST:event_jbtThoatActionPerformed

    private void jbtThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtThemActionPerformed
        // TODO add your handling code here:
        ThemPhieuXuatHangDialog dlg = new ThemPhieuXuatHangDialog(null, true);
        dlg.setVisible(true);

//        if(dlg.getIsSet()){
//            this.list.add(dlg.getMh());
//            tongTien += dlg.getMh().getThanhTien();
//            this.jtfTongTien.setText(String.valueOf(tongTien));
//            this.hienThiDanhSachMatHang(list);
//        }
    }//GEN-LAST:event_jbtThemActionPerformed

    private void jbtSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtSuaActionPerformed
        // TODO add your handling code here:
//        int dong = this.jtbDanhSach.getSelectedRow();
//        if(dong>=0){
//            CapNhatMatHangTrongPhieuXuatHangDialog dlg = new CapNhatMatHangTrongPhieuXuatHangDialog(null, true);
//            HoaDonPOJO hd = new HoaDonPOJO();
//            hd.setMaMatHang(list.get(dong).getMaMatHang());
//            hd.setTenMatHang((String)this.jtbDanhSach.getValueAt(dong, 1));
//            hd.setDonviTinh((String)this.jtbDanhSach.getValueAt(dong, 2));
//            hd.setSoLuong((Integer)this.jtbDanhSach.getValueAt(dong, 3));
//            hd.setDonGia((Double)this.jtbDanhSach.getValueAt(dong, 4));
//            hd.setThanhTien((Double)this.jtbDanhSach.getValueAt(dong, 5));
//            dlg.setMh(hd);
//            dlg.setVisible(true);
//            //
//            list.get(dong).setMaMatHang(dlg.getMh().getMaMatHang());
//            list.get(dong).setTenMatHang(dlg.getMh().getTenMatHang());
//            list.get(dong).setDonGia(dlg.getMh().getDonGia());
//            list.get(dong).setDonviTinh(dlg.getMh().getDonviTinh());
//            list.get(dong).setSoLuong(dlg.getMh().getSoLuong());
//            list.get(dong).setThanhTien(dlg.getMh().getThanhTien());
//            this.hienThiDanhSachMatHang(list);
//        }
    }//GEN-LAST:event_jbtSuaActionPerformed

    private void jbtXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtXoaActionPerformed
        // TODO add your handling code here:
        int reply = JOptionPane.showConfirmDialog(this, "Bạn có chắc là muốn xóa?", "Thông báo", WIDTH);
        if(reply == JOptionPane.OK_OPTION){
//            int dong = this.jtbDanhSach.getSelectedRow();
//            if(dong>=0){
//                tongTien -= list.get(dong).getThanhTien();
//                this.jtfTongTien.setText(String.valueOf(tongTien));
//                list.remove(dong);
//                this.hienThiDanhSachMatHang(list);
//            }
        }
        
    }//GEN-LAST:event_jbtXoaActionPerformed

    private void jbtChapNhanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtChapNhanActionPerformed
        // TODO add your handling code here:
//        if(list.isEmpty()){
//            JOptionPane.showMessageDialog(this, "Chưa có mặt hàng.");
//        }else{
//            Vector vt = (Vector)this.jcbTenDaiLy.getSelectedItem();
//            int maDaiLy = (Integer)vt.get(0);
//
//            String ngay = this.jtfNgayLapPhieu.getText();
//            String array_ngay[] = ngay.split("-");
//            String ngayLapPhieu = "";
//            for(int i=3; i>0; i--){
//                ngayLapPhieu += array_ngay[i-1];
//                if(i!=1){
//                    ngayLapPhieu += "-";
//                }
//            }
//
//            String n = HoaDonBUS.insertHoaDon(list, ngayLapPhieu , maDaiLy, tongTien);
//            if(n.equals("")){
//                    JOptionPane.showMessageDialog(this, "Thêm thành công!!!");
//                    DaiLyBUS.updateTienNoAll();
//                    this.dispose();
//            } else {
//                    JOptionPane.showMessageDialog(this, n);
//            }

//        }
    }//GEN-LAST:event_jbtChapNhanActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtChapNhan;
    private javax.swing.JButton jbtSua;
    private javax.swing.JButton jbtThem;
    private javax.swing.JButton jbtThoat;
    private javax.swing.JButton jbtXoa;
    private javax.swing.JComboBox jcbTenDaiLy;
    private javax.swing.JLabel jlbNgayLapPhieu;
    private javax.swing.JLabel jlbTenDaiLy;
    private javax.swing.JLabel jlbTongTien;
    private javax.swing.JTable jtbDanhSach;
    private javax.swing.JTextField jtfNgayLapPhieu;
    private javax.swing.JTextField jtfTongTien;
    // End of variables declaration//GEN-END:variables

}
